package com.site.wisdom_construction.controller.healthEpidemicModel;

import com.site.wisdom_construction.entity.healthEpidemicModel.AntiepidemicWristband;
import com.site.wisdom_construction.entity.healthEpidemicModel.WarningRecord;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.healthEpidemicModel.AntiepidemicWristbandService;
import com.site.wisdom_construction.service.healthEpidemicModel.WarningRecordService;
import com.site.wisdom_construction.statisticsResult.WarningRecordCount;
import com.site.wisdom_construction.util.TimeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2022/1/7
 * @Description
 */
@Api(tags = "定位手环预警记录接口")
@RestController
@RequestMapping("WarningRecord")
@CrossOrigin
public class WarningRecordController {
    @Autowired
    private WarningRecordService warningRecordService;

    @Autowired
    private AntiepidemicWristbandService antiepidemicWristbandService;

    @ApiOperation(value = "增加定位手环预警记录", notes = "测试数据:")
    @PostMapping("/insertWarningRecord")
    public JsonResult insertWarningRecord(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        map.put("updateTime", TimeUtils.getNowTimeS());
        int i = warningRecordService.insertWarningRecord(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功录入定位手环预警记录!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("录入定位手环预警记录失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除定位手环预警记录", notes = "测试数据:")
    @PostMapping("/deleteWarningRecord")
    public JsonResult deleteWarningRecord(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        List<Integer> list = (List<Integer>) map.get("list");
        int l = 0;
        Map mapR = new HashMap();
        for (int i = 0;i<list.size();i++){
            mapR.put("id",list.get(i));
            int a = warningRecordService.deleteWarningRecord(mapR);
            l+=a;
        }
        if (l>=1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功删除定位手环预警记录!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除定位手环预警记录失败!");
        }
        return jsonResult;
    }


    @ApiOperation(value = "修改定位手环预警记录", notes = "测试数据:")
    @PostMapping("/updateWarningRecord")
    public JsonResult updateWarningRecord(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        map.put("updateTime", TimeUtils.getNowTimeS());
        int i = warningRecordService.updateWarningRecord(map);
        if (i==1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功修改定位手环预警记录!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改定位手环预警记录!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页模糊查询定位手环预警记录", notes = "测试数据:")
    @PostMapping("/findWarningRecordByPage")
    public Page findWarningRecordByPage(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(warningRecordService.Total(map));
        List<WarningRecord> wristbandList = warningRecordService.findWarningRecordByPage(map);
        for (int i = 0;i<wristbandList.size();i++){
            WarningRecord warningRecord = wristbandList.get(i);
            map.put("equipmentNumber",warningRecord.getEquipmentNumber());
            AntiepidemicWristband antiepidemicWristband = antiepidemicWristbandService.findAntiepidemicWristbandByEquipment(map);
            warningRecord.setWorkerName(antiepidemicWristband.getWorkerName());
            warningRecord.setWorkerCard(antiepidemicWristband.getWorkerCard());
            warningRecord.setWorkerPhone(antiepidemicWristband.getWorkerPhone());
        }
        page.setItems(wristbandList);
        return page;
    }

    @ApiOperation(value = "当月预警统计", notes = "测试数据:")
    @PostMapping("/countWarningRecordByTime")
    public OneDataResult countWarningRecordByTime(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(warningRecordService.countWarningRecordByTime(map));
        return oneDataResult;
    }

    @ApiOperation(value = "今日预警", notes = "测试数据:")
    @PostMapping("/countWarningRecordByWarningType")
    public OneDataResult countWarningRecordByWarningType(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        WarningRecordCount warningRecordCount = new WarningRecordCount();
        int tW = warningRecordService.countWarningRecordByWarningTypeTw(map);
        int xL = warningRecordService.countWarningRecordByWarningTypeXl(map);
        int xY = warningRecordService.countWarningRecordByWarningTypeXy(map);
        int XY = warningRecordService.countWarningRecordByWarningTypeXY(map);
        warningRecordCount.settW(tW);
        warningRecordCount.setxL(xL);
        warningRecordCount.setxY(xY);
        warningRecordCount.setXY(XY);
        warningRecordCount.setAll(tW+xL+xY+XY);
        oneDataResult.setData(warningRecordCount);
        return oneDataResult;
    }
}
